package com.yangxk.kadmin.common.beans;

import com.yangxk.kadmin.common.enumeration.BaseExceptionEnum;
import com.yangxk.kadmin.common.enumeration.CommonExceptionEnum;
import com.fasterxml.jackson.annotation.JsonInclude;

/**
 * 接口返回结果
 *
 * @author yangxk
 * @date 2018/9/5 21:58
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result<T> {
    private int code;
    private String msg;
    private T data;

    private Result(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    private Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public int getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }


    public static <T> Result<T> ofSuccess() {
        return new Result<T>(CommonExceptionEnum.SUCCESS.getCode(), CommonExceptionEnum.SUCCESS.getMessage());
    }

    public static <T> Result<T> ofSuccess(T data) {
        return new Result<T>(CommonExceptionEnum.SUCCESS.getCode(), CommonExceptionEnum.SUCCESS.getMessage(), data);
    }

    public static <T> Result<T> ofError() {
        return new Result<T>(CommonExceptionEnum.ERROR.getCode(), CommonExceptionEnum.ERROR.getMessage());
    }

    public static <T> Result<T> ofError(BaseExceptionEnum code) {
        return new Result<T>(code.getCode(), code.getMessage());
    }

    public static <T> Result<T> ofError(int code, String message) {
        return new Result<T>(code, message);
    }
}
